package com.apkcore.bl;

/**
 * https://leetcode-cn.com/problems/merge-two-sorted-lists/
 */
public class _21排序列表的合并 {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode flag = new ListNode(0);
        ListNode firstNode = flag;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                flag.next = l1;
                l1 = l1.next;

            } else {
                flag.next = l2;
                l2 = l2.next;
            }
            flag = flag.next;
        }
        flag.next = l1 == null ? l2 : l1;
        return firstNode.next;
    }

    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

}

